package com.pactera.commons;

public class RestResult extends BaseObject {
    private static final long serialVersionUID = -5816304413090422336L;
    private int code;
    private String msg;
    private Object data;

    /**
     * 成功
     *
     * @param msg  消息
     * @param data 返回的数据
     * @Author: liqingdian
     */
    public static RestResult succeed(String msg, Object data) {
        return new RestResult(RestStatus.SUCCESS.code(), msg, data);
    }

    /**
     * 成功
     *
     * @param data 返回的数据
     * @Author: liqingdian
     */
    public static RestResult succeed(Object data) {
        return new RestResult(RestStatus.SUCCESS.code(), "success", data);
    }

    /**
     * 成功
     *
     * @Author: liqingdian
     */
    public static RestResult succeed() {
        return succeed(null);
    }

    /**
     * 失败
     *
     * @param msg  失败消息
     * @param data 失败数据
     * @Author: liqingdian
     */
    public static RestResult failed(int code, String msg, Object data) {
        return new RestResult(code, msg, data);
    }

    /**
     * 失败
     *
     * @param msg  失败消息
     * @param data 失败数据
     * @Author: liqingdian
     */
    public static RestResult failed(String msg, Object data) {
        return failed(RestStatus.OTHER_ERROR.code(), msg, data);
    }

    /**
     * 失败
     *
     * @param msg 失败消息
     * @Author: liqingdian
     */
    public static RestResult failed(String msg) {
        return failed(msg, null);
    }

    public RestResult() {
    }

    public RestResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public RestResult setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public RestResult setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public RestResult setData(Object data) {
        this.data = data;
        return this;
    }

    /**
     * 将data对象按存入时类型强转
     *
     * @Author: liqingdian
     */
    public <T> T parseData(Class<T> clazz) {
        if (this.data != null) {
            return (T) this.data;
        }
        return null;
    }

}
